package Interfaces;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JTextField;

public class CampoTesto
{
JTextField Campo; 
String testo;

public JTextField crea(String t)
{
	Campo = new JTextField();
	return crea_aux(t);
}

public JTextField crea(String t, int n)
{
	Campo = new JTextField("",n);
	return crea_aux(t);
}

public JTextField crea_aux(String t)
{
	testo=t;
	Campo.setText(testo);
	Campo.addMouseListener(new MouseListener(){
		@Override public void mouseClicked(MouseEvent e) {
			if(Campo.getText().equals(testo))
				Campo.setText("");
		}
		@Override public void mouseEntered(MouseEvent e) {}
		@Override public void mouseExited(MouseEvent e) {}
		@Override public void mousePressed(MouseEvent e) {}
		@Override public void mouseReleased(MouseEvent e) {}
		});
	
	Campo.addKeyListener(new KeyListener(){
		@Override public void keyPressed(KeyEvent e) {}
		@Override public void keyReleased(KeyEvent e) {
		if(Campo.getText().equals(""))
			{
			Campo.setText(testo);
			Campo.selectAll();
			}				
		}
		@Override public void keyTyped(KeyEvent e) {}
		});
	return Campo;
}
public JTextField get()
{
	return Campo;
}
public void setText(String string)
{
	Campo.setText(string);
}
public String getText()
{
	return Campo.getText();
}
public void clear() 
{
	Campo.setText(testo);
	
}
}
